import { getLyric, getSongDetail } from '@/services/music/api';
import { getRandomNumber } from '@/utils/math';
import { parseLyric } from '@/utils/parseLyric';
import { useEffect, useState } from 'react';

// Partial 所有类型变成可选类型
export default () => {
  const [initData, setInitData] = useState<MUSIC.InitDataType>({
    currentSong:{
    },
    playList: [], // 歌曲列表
    currentSongIndex: 0,
    order: 0, //0 循环播放 1 随机 2 单曲循环
    lyricList: [], // 歌词列表
    lyricing: '加油小兵', // 正在唱的歌词
  });

  console.log("我打印了几次",initData);


  //请求歌词信息
  const chnageLyricListAction = (lyricList: MUSIC.LyricListType[]) => {
    setInitData({ ...initData, lyricList });
  };
  //正在播放的歌词
  const changeLyricingAction = (lyricing: string) => {
    setInitData({ ...initData, lyricing });
  };

  //获取歌曲详情
  const changeCurrentSongAction = async (song: any) => {
    console.log('song', song);
    setInitData({ ...initData, currentSong: {...song} });
  };
  //获取歌曲列表
  const changePlayListAction = (playList: MUSIC.PlayListType[]) => {
    setInitData({ ...initData, playList });
  };

  //获取当前播放歌曲的下标
  const changeCurrentSongIndexAction = (currentSongIndex: number) => {
    setInitData({ ...initData, currentSongIndex });
  };
  //播放顺序
  const changeOrderAction = (order: number) => {
    setInitData({ ...initData, order });
  };

  // 获取歌词列表
  const getLyricAction = (id: number) => {
    getLyric(id).then((res) => {
      if (res.lrc) {
        const lyricList: MUSIC.LyricListType[] = parseLyric(res.lrc.lyric);
        chnageLyricListAction(lyricList);
      }
    });
  };
  const getSongDetailAction = (paramId: number) => {
    //1.根据id查找playList里是否已经有了该歌曲
    const playList = initData.playList;
    const songIndex = playList.findIndex((item) => {
      return item.id === paramId;
    });
    //   2.判断是否在播放列表里找到了歌曲
    let song = null;
    if (songIndex !== -1) {
      //找到了歌曲 将当前歌曲播放
      changeCurrentSongIndexAction(songIndex);
      song = playList[songIndex];
      changeCurrentSongAction(song);
    } else {
      //未找到歌曲
      getSongDetail(paramId).then((res: MUSIC.PlaylistTrackBackT) => {
        song = res.songs && res.songs[0];
        if (!song) return;
        //把歌曲保存到播放列表里
        const newPlayLiat = [...playList];
        newPlayLiat.push(song);
        //更新redux里playList里的值
        changePlayListAction(newPlayLiat);
        //更新当前歌曲下标
        changeCurrentSongIndexAction(newPlayLiat.length - 1);
        changeCurrentSongAction(song);
        //请求歌词
        if (!song || !song.id) return;
        getLyricAction(song.id);
      });
    }
  };
  //上一首(-1) or 下一首(1)
  const changeCurrentSong = (tag: number) => {
    const { order, playList, currentSong } = initData;
    let songIndex = playList.findIndex((item) => item.id === currentSong.id);
    debugger;
    switch (order) {
      case 1:
        let randomIndex = getRandomNumber(playList.length); //随机播放
        //当型循环 当随机出来的数跟正在播放音乐的下标相同时重新随机直到不相同结束循环
        if (playList.length > 1) {
          while (randomIndex === songIndex) {
            randomIndex = getRandomNumber(playList.length);
          }
        } else {
          randomIndex = 0;
        }
        songIndex = randomIndex;
        changeCurrentSongIndexAction(songIndex);
        break;
      default: //顺序播放 and 单曲循环
        songIndex += tag;
        if (songIndex >= playList.length) {
          //当等于音乐列表长度时切换到第一首歌
          songIndex = 0;
        }
        if (songIndex < 0) {
          //当小于0时切换到最后一首歌
          songIndex = playList.length - 1;
        }
        changeCurrentSongIndexAction(songIndex);
    }
    const cs = playList[songIndex];
    if (!cs) return;
    changeCurrentSongAction(cs);
    getLyricAction(cs.id);
  };

  return {
    changeCurrentSong,
    changeLyricingAction,
    changeOrderAction,
    getSongDetailAction,
    changeCurrentSongAction,
    changePlayListAction,
    getLyricAction,
    initData,
  };
};
